//
//  QCDrivingCell.swift
//  爱汽车
//
//  Created by liujianlong on 2023/8/25.
//

import UIKit

class QCDrivingCell: UITableViewCell {
    @IBOutlet var title: UILabel!
    @IBOutlet var content: UILabel!
    @IBOutlet var addBtn: UIButton!
    @IBOutlet var subBtn: UIButton!
    var limit = (max:0,min:0)
    var block: ((_ value:Int)->Void)?
    override func awakeFromNib() {
        super.awakeFromNib()
        addBtn.addGestureRecognizer(add_longpress)
        subBtn.addGestureRecognizer(sub_longpress)
    }
    lazy var add_longpress : UILongPressGestureRecognizer = {
        let press = UILongPressGestureRecognizer(target: self, action: #selector(actionForAddlongPress))
        return press
    }()
    lazy var sub_longpress : UILongPressGestureRecognizer = {
        let press = UILongPressGestureRecognizer(target: self, action: #selector(actionForSublongPress))
        return press
    }()
    
    var value = 0 {
        didSet{
            content.text = "\(value)"
        }
    }
    @IBAction func actionForBtn(_ sender: UIButton) {
        value = sender.currentTitle ?? "" == "+" ? min(value + 1, limit.max) : max(value - 1, limit.min)
        block?(value)
    }
    @objc func actionForAddlongPress(sender: UILongPressGestureRecognizer){
        if sender.state == .changed{
            value = min(value + 1, limit.max)
        }
        if sender.state == .ended{
            block?(value)
        }
    }
    @objc func actionForSublongPress(sender: UILongPressGestureRecognizer){
        if sender.state == .changed{
            value = max(value - 1, limit.min)
        }
        if sender.state == .ended{
            block?(value)
        }
    }
}
